package com.laiyima.zhongjiang.linghuilv.demo;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class SmsPasswordActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView imageView;
    private EditText number,captch;
    private TextView getSms;
    private Button smsNext;
    private String TAG="SmsPasswordActivity";
    private int smsCode;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms_password);
        imageView=findViewById(R.id.sms_back);
        number=findViewById(R.id.sms_number);
        captch=findViewById(R.id.sms_captch);
        getSms=findViewById(R.id.sms_getCap);
        smsNext=findViewById(R.id.sms_next);

        imageView.setOnClickListener(this);
        getSms.setOnClickListener(this);
        smsNext.setOnClickListener(this);

        captch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String s1=captch.getText().toString();
                String s2=number.getText().toString();
                if(s1.length()==6 && s2.length()==11){
                    smsNext.setEnabled(true);
                    smsNext.setTextColor(Color.WHITE);
                    smsNext.setBackgroundResource(R.drawable.begin_textview_shape_change);
                }else {
                    smsNext.setEnabled(false);
                    smsNext.setTextColor(Color.parseColor("#B8B8B8"));
                    smsNext.setBackgroundResource(R.drawable.firist_edittext_shape);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


    }

    @Override
    public void onClick(View v) {
        int id=v.getId();
        switch (id){
            case R.id.sms_back:
                finish();
                break;
            case R.id.sms_getCap:
                String myNUmber=number.getText().toString();
                if(TextUtils.isEmpty(myNUmber)){
                    Toast.makeText(getBaseContext().getApplicationContext(), "请输入号码", Toast.LENGTH_LONG).show();
                    return;
                }else if(myNUmber.length()<11){
                Toast.makeText(getBaseContext().getApplicationContext(), "请输入正确号码", Toast.LENGTH_LONG).show();
                return;
                }if(myNUmber.length()==11){
                mCountDownTimer.start();
                RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/api/Register/sms_password");
                //需要携带的参数
                params.addBodyParameter("mobile", myNUmber);
                //  params.addBodyParameter("code", str);
                x.http().post(params, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        Log.e(TAG, "result        "+result);
                        try{
                            JSONObject jsonObject = new JSONObject(result);
                            smsCode = jsonObject.optInt("code", 0);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        Log.e(TAG, "Verificationcode"+smsCode);
                        if (smsCode==200){

                        }
                        if (smsCode==201){
                            mCountDownTimer.cancel();
                            getSms.setEnabled(true);
                            getSms.setText("发送验证码");
                            Toast.makeText(getBaseContext().getApplicationContext(), "该手机号码未注册", Toast.LENGTH_SHORT).show();
                        }
                        if (smsCode==501){
                            mCountDownTimer.cancel();
                            getSms.setEnabled(true);
                            getSms.setText("发送验证码");
                            Log.e(TAG, "错误的手机号码");
                            Toast.makeText(getBaseContext().getApplicationContext(), "错误的手机号码", Toast.LENGTH_SHORT).show();
                        }
                    }
                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Log.e(TAG, "onError");
                    }

                    @Override
                    public void onCancelled(org.xutils.common.Callback.CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {
                    }

                });
                }
                break;
            case R.id.sms_next:
                String buNumber=number.getText().toString();
                String myCaptch=captch.getText().toString();
                if(TextUtils.isEmpty(buNumber)){
                    Toast.makeText(getBaseContext().getApplicationContext(), "请输入号码", Toast.LENGTH_LONG).show();
                    return;
                }else if(buNumber.length()<11){
                    Toast.makeText(getBaseContext().getApplicationContext(), "请输入正确号码", Toast.LENGTH_LONG).show();
                    return;
                }
                else if(TextUtils.isEmpty(myCaptch)){
                    Toast.makeText(getBaseContext().getApplicationContext(), "请输入验证码", Toast.LENGTH_LONG).show();
                    return;
                }else if(myCaptch.length()<6 || myCaptch.length()>6){
                    Toast.makeText(getBaseContext().getApplicationContext(), "请输入正确验证码", Toast.LENGTH_LONG).show();
                    return;
                }else{
                    Intent intent = new Intent(SmsPasswordActivity.this, ModifyPasswordActivity.class);
                    intent.putExtra("number", buNumber);
                    intent.putExtra("captch", myCaptch);
                    startActivity(intent);
                }
                break;

        }
    }


    private CountDownTimer mCountDownTimer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long l) {
            getSms.setEnabled(false);
            getSms.setText((l / 1000 )+ "秒后可重发");
        }

        @Override
        public void onFinish() {
            getSms.setEnabled(true);
            getSms.setText("发送验证码");
        }
    };
}
